package com.dianqu.weather.core.service;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.dianqu.weather.core.common.ContextHolderUtils;
import com.dianqu.weather.core.common.SystemConfig;
import com.dianqu.weather.core.dao.ClientDao;
import com.dianqu.weather.core.entity.TClient;
import com.dianqu.weather.core.entity.TUser;

@Service
public class ClientService {

	private final Logger logger = Logger.getLogger(getClass());
	@Autowired
	private ClientDao clientDao;

	@Async
	public void addLog(HttpServletRequest request, String location) {
		HttpSession session = request.getSession();
		TClient log = new TClient();
		log.setIp(ContextHolderUtils.getIpAddress(request));
		log.setLogDate(new Date());
		log.setQuery(location);
		if (session != null) {// notice
			Object obj = session.getAttribute(SystemConfig.LOGIN_USER);
			if (obj != null) {
				log.setUser((TUser) obj);
			}
		}
		try {
			clientDao.save(log);
			logger.info(log.toString());
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("add logger to database failed!");
		}
	}

}
